package com.mySampleApplication.client.CodeRuleAtter;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mySampleApplication.client.components.SDataSource;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeRuleAtterLayout extends VLayout {
    public CodeRuleAtterLayout(ListGridRecord frecord) {
        // 创建一个垂直布局容器
        VLayout layout = new VLayout(10); // 参数15为布局成员之间的间距
        layout.setWidth100();
        layout.setHeight100();
        layout.setPadding(10);
//        DataSource dataSource = SDataSource.getDataSource();
        Map<String, Object> listMap = CodeRuleAtter.getListGrid(frecord);
        ListGrid listGrid = (ListGrid) listMap.get("listGrid");
        List<ListGridRecord> recordList = new ArrayList<>();
        Map<String, String> mapPage = new HashMap<>();
        mapPage.put("pageSize", "1");
        mapPage.put("pageNumber", "10");
        Map<String, String> mapf =new HashMap<>();
        if(frecord!=null){
            mapf.put("coderule_id",frecord.getAttribute("id"));
            CodeRuleAtterService.App.getInstance().getSelect(mapf, mapPage, new AsyncCallback<CodeRuleAtterDto>() {
                public void onSuccess(CodeRuleAtterDto result) {
                    List<Map<String, String>> keyValueMap = result.getKeyValueMap();
                    for (Map<String, String> data : keyValueMap) {
                        ListGridRecord record = new ListGridRecord();
                        data.forEach((k, v) -> {
                            record.setAttribute(k, v);
                        });
                        // 设置其他需要的属性
                        recordList.add(record);

                    }
                    listGrid.setData(recordList.toArray(new ListGridRecord[0]));
                    //SC.say( "获取数据成功！"+modelId);
                    //list.addAll(result);
                }

                public void onFailure(Throwable caught) {
                    SC.warn("获取数据失败！");
                }
            });
        }



        // 设置数据源到列表网格
//         listGrid.setDataSource(dataSource);
        // 加载数据
        listGrid.fetchData();
        // 组装页面
        //layout.addMember(searchArea);
        layout.addMember((HLayout) listMap.get("buttonLayout"));
        // 添加列表网格到布局容器中
        layout.addMember(listGrid);

        // 添加布局容器到页面中
        this.addChild(layout);


    }
}
